package com.ruoyi.business.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.SysArea;
import com.ruoyi.business.domain.vo.SysAreaVo;
import com.ruoyi.business.mapper.SysAreaMapper;
import com.ruoyi.business.service.ISysAreaService;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.service.AreaService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.redis.CacheUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 地区服务层 服务层
 * @Author yyt
 */
@Service
@RequiredArgsConstructor
public class SysAreaServiceImpl implements ISysAreaService, AreaService {

    private final SysAreaMapper baseMapper;
    @Override
    public List<SysAreaVo> selectVoArea() {
        LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
        return baseMapper.selectVoList(lqw);
    }

    @Cacheable(cacheNames = CacheNames.SYS_AREA, key = "#areaId.toString()")
    @Override
    public String selectAreaNameById(Long areaId) {
        if (areaId == null){
            return StringUtils.EMPTY;
        }
        SysArea sysArea = baseMapper.selectById(areaId);
        if (ObjectUtil.isNull(sysArea)) {
            return StringUtils.EMPTY;
        }

        return sysArea.getAreaName();
    }

    @Override
    public List<SysArea> selectAreaList() {
        return baseMapper.selectList();
    }

    @Override
    public void loadingAreaCache() {
        List<SysArea> areaList = selectAreaList();
        areaList.forEach(area ->
            CacheUtils.put(CacheNames.SYS_AREA, String.valueOf(area.getAreaId()), area.getAreaName()));
    }
}
